package ui;

import info.Manuscript;
import io.ManuscriptAccess;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

import org.jdom2.JDOMException;

/**
 * Shows a list of Manuscripts based on user permissions.
 * 
 * @author Aaron Chen
 * @author Lun-Wei Chang
 * @version 3/14/2014
 */
@SuppressWarnings("serial")
public class ManuscriptListPanel extends AbstractMainComponent {
  
  /** The index value of the manuscript author column. */
  private static final int COLUMN_AUTHOR_INDEX = 0;
  /** The index value of the manuscript title column. */
  private static final int COLUMN_TITLE_INDEX = 1;
  /** The index value of the manuscript abstract column. */
  private static final int COLUMN_ABSTRACT_INDEX = 2;
  /** The index value of the manuscript abstract column. */
  private static final int COLUMN_ACCEPTANCE_INDEX = 3;
  /** The index value of the manuscript abstract column. */
  private static final int COLUMN_SPC_INDEX = 4;
  /** The index value of the manuscript viewing button column. */
  private static final int COLUMN_BUTTON_INDEX = 5;
  
  /** The header labels for the table's columns. */
  private static final String[] HEADERS = {
    "Author",
    "Manuscript Title",
    "Abstract",
    "Acceptance Status",
    "Subprogram Chair",
    ""
  };

  /**
   * Create the panel that holds manuscript listings.
   * @param the_parent The main application window container.
   */
  public ManuscriptListPanel(final DisplayPanel the_parent) {
    super(the_parent);
    setLayout(new BorderLayout());
    setBackground(Standards.COLOR_BACKGROUND);
    final JTable manuscript_table = createManuscriptTable();
    final JScrollPane scroll_pane = createScrollPane(manuscript_table);
    add(manuscript_table.getTableHeader(), BorderLayout.NORTH);
    add(scroll_pane, BorderLayout.CENTER);
  }
  
  /**
   * Creates a scroll pane specially suited for the table.
   * @param the_component The table to be made scrollable.
   * @return The scroll pane.
   */
  private JScrollPane createScrollPane(final Component the_component) {
    final JScrollPane scroll_pane = new JScrollPane(the_component);
    scroll_pane.getViewport().setBackground(Standards.COLOR_BACKGROUND);
    scroll_pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroll_pane.getVerticalScrollBar().setUnitIncrement(Standards.SCROLL_BAR_INCREMENT);
    scroll_pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    return scroll_pane;
  }
  
  /**
   * Creates a new JTable with brief manuscript information in each row and a
   * button for viewing each manuscript at the end.
   * @return A JTable with one manuscript per row.
   */
  private JTable createManuscriptTable() { 
    final ManuscriptAccess database = getManuscriptAccess();
    final List<Manuscript> list = database.getAllManuscripts();
    final Object[][] data = new Object[list.size()][HEADERS.length];
    final boolean[] bools = {false, false, false, true, false, true};
    final ActionListener[][] als = new ActionListener[list.size()][HEADERS.length];
    final Iterator<Manuscript> itr = list.iterator();
    for (int i = 0; i < data.length; i++) {
      final Manuscript m = itr.next();
      data[i][COLUMN_AUTHOR_INDEX] = m.getSubmittingAuthor();
      data[i][COLUMN_TITLE_INDEX] = m.getTitle();
      data[i][COLUMN_ABSTRACT_INDEX] = m.getAbstract();
      data[i][COLUMN_ACCEPTANCE_INDEX] = m.getDecisionStatusString();
      data[i][COLUMN_SPC_INDEX] = database.getSPCAssignedToManuscript(m.getID());
      data[i][COLUMN_BUTTON_INDEX] = "View Manuscript";
      als[i][COLUMN_BUTTON_INDEX] = new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent the_e) {
          final DisplayPanel dsp = getDisplayStack();
          final ManuscriptSubmissionPanel msp =
              new ManuscriptSubmissionPanel(dsp, database.getManuscript(m.getID()));
          dsp.addToStack(msp);
        }
      };
    }
    return new CustomTable(data, HEADERS, bools, als);
  }
  
  /**
   * Attempts to create a ManuscriptAccess connection to the database.
   * @return The ManuscriptAccess object.
   */
  private ManuscriptAccess getManuscriptAccess() {
    ManuscriptAccess database = null;
    try {
      database = new ManuscriptAccess();
    } catch (final IOException e) {
      JOptionPane.showMessageDialog(this, "There is a problem accessing the database.");
      e.printStackTrace();
    } catch (final JDOMException e) {
      JOptionPane.showMessageDialog(this, "The file specified by the code is incorrect.");
      e.printStackTrace();
    }
    return database;
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public String getPanelLabel() {
    return "List of Manuscripts";
  }

}
